/*
 * Home.cpp
 *
 *  Created on: 11/06/2011
 *      Author: dela
 */

#include "Home.h"

#include <iostream>
#include "../Connection.h"
#include "ClassWindow.h"
#include "CourseWindow.h"

using namespace std;

/**
 *
 */
Home::Home(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder) :
	Gtk::Window(cobject), builder(builder) {
	// TreeView:
	Gtk::TreeView *treeView;
	builder->get_widget("treeview1", treeView);
	treeModel = Gtk::ListStore::create(mColumns);
	treeView->set_model(treeModel);
	setTreeModelInfo();

	// Accion al hacer doble click:
	treeView->signal_row_activated().connect(
			sigc::mem_fun(*this, &Home::onRowSelected));

	// Boton crear clase:
	Gtk::Button *button = 0;
	builder->get_widget("button2", button);
	button->signal_clicked().connect(
			sigc::mem_fun(*this, &Home::onCreateClass));

	// Boton crear curso:
	builder->get_widget("button1", button);
	button->signal_clicked().connect(
			sigc::mem_fun(*this, &Home::onCreateCourse));

	// Boton refrescar:
	builder->get_widget("button3", button);
	button->signal_clicked().connect(
			sigc::mem_fun(*this, &Home::setTreeModelInfo));

	// Refrescar cuando gana foco la ventan:
	this->signal_focus_in_event().connect(
			sigc::mem_fun(*this, &Home::onFocus));
}


/**
 *
 */
bool Home::onFocus(GdkEventFocus* e) {
	setTreeModelInfo();
	return true;
}


/**
 *
 */
void Home::onRowSelected(const Gtk::TreeModel::Path &path,
		Gtk::TreeViewColumn *column) {
	Gtk::TreeModel::iterator iter = treeModel->get_iter(path);
	Gtk::TreeModel::Row row = *iter;
	unsigned long idSelected = row[mColumns.id];
	Glib::ustring type = row[mColumns.type];
	if (type == "Clase") {
		list<Class>::iterator it;
		Class *c;
		for (it=classes.begin(); it!=classes.end(); it++) {
			if (it->getId() == idSelected) {
				c = &(*it);
			}
		}

		// Verify password:
		bool pass = false;
		if (c->getHasPassword()) {
			//Dialogo de ingreso de password:
			Gtk::Dialog *dialog;
			builder->get_widget("dialog1", dialog);
			Gtk::Entry *entryPass;
			builder->get_widget("entry1", entryPass);
			entryPass->set_text("");
			int ret = dialog->run();
			if ( ret == 1 && c->getPassword() == entryPass->get_text() ) {
				pass = true;
			} else {
				if (ret == 1) {
					Gtk::MessageDialog dialog(*this, "Acceso restringido!");
					dialog.set_secondary_text("Contraseña invalida.");
					dialog.run();
				}
			}
			dialog->hide();
		} else
			pass = true;

		if (pass) {
			string path;
			if (Connection::getInstance()->getType() == 'I')
				path = "glade/instructor/class.glade";
			else
				path = "glade/student/class.glade";
			Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file(path);
			ClassWindow* cw = 0;
			builder->get_widget_derived("window1", cw);
			cw->setClass(*c);

			//No se puede editar el id:
			Gtk::Entry *input;
			builder->get_widget("entry1", input);
			input->set_sensitive(false);

			cw->show();
		}
	} else {
		list<Course>::iterator it;
		Course *c;
		for (it=courses.begin(); it!=courses.end(); it++) {
			if (it->getId() == idSelected) {
				c = &(*it);
			}
		}

		// Verify password:
		bool pass = false;
		if (c->getHasPassword()) {
			//Dialogo de ingreso de password:
			Gtk::Dialog *dialog;
			builder->get_widget("dialog1", dialog);
			Gtk::Entry *entryPass;
			builder->get_widget("entry1", entryPass);
			entryPass->set_text("");
			int ret = dialog->run();
			if ( ret == 1 && c->getPassword() == entryPass->get_text() ) {
				pass = true;
			} else {
				if (ret == 1) {
					Gtk::MessageDialog dialog(*this, "Acceso restringido!");
					dialog.set_secondary_text("Contraseña invalida.");
					dialog.run();
				}
			}
			dialog->hide();
		} else
			pass = true;

		if (pass) {
			string path;
			if (Connection::getInstance()->getType() == 'I')
				path = "glade/instructor/course.glade";
			else
				path = "glade/student/course.glade";
			Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file(path);
			CourseWindow* cw = 0;
			builder->get_widget_derived("window1", cw);
			cw->setCourse(*c);

			//No se puede editar el id:
			Gtk::Entry *input;
			builder->get_widget("entry1", input);
			input->set_sensitive(false);

			cw->show();
		}
	}
}


/**
 *
 */
void Home::setTreeModelInfo() {
	treeModel->clear();

	Gtk::TreeModel::Row row;
	courses.clear();
	Connection::getInstance()->getCourses(courses);
	classes.clear();
	Connection::getInstance()->getClasses(classes);

	list<Course>::iterator it;
	for (it=courses.begin(); it!=courses.end(); it++) {
		row = *(treeModel->append());
		row[mColumns.id] = it->getId();
		row[mColumns.type] = "Curso";
		row[mColumns.name] = it->getName();
		row[mColumns.startDate] = it->getStartDate();
	}

	list<Class>::iterator itClass;
	for (itClass=classes.begin(); itClass!=classes.end(); itClass++) {
		if ( itClass->getCourseId() == 0 ) {
			row = *(treeModel->append());
			row[mColumns.id] = itClass->getId();
			row[mColumns.type] = "Clase";
			row[mColumns.name] = itClass->getName();
			row[mColumns.startDate] = itClass->getStartDate();
		}
	}
}


/**
 *
 */
void Home::onCreateClass() {
	string path;
	if (Connection::getInstance()->getType() == 'I')
		path = "glade/instructor/class.glade";
	else
		path = "glade/student/class.glade";
	Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file(path);
	ClassWindow* cw = 0;
	builder->get_widget_derived("window1", cw);
	cw->show();
}


/**
 *
 */
void Home::onCreateCourse() {
	string path;
	if (Connection::getInstance()->getType() == 'I')
		path = "glade/instructor/course.glade";
	else
		path = "glade/student/course.glade";
	Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file(path);
	CourseWindow* cw = 0;
	builder->get_widget_derived("window1", cw);
	cw->show();
}
